package cn.stronglink.assetmanage.business.inspection.adapter;

import android.app.Application;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.index.view.onRecyclerListener;
import cn.stronglink.assetmanage.utils.Constants;

/**
 * Created by zhengjinglan on 2018/6/21.
 */

public class RepairAdapter extends RecyclerView.Adapter<RepairAdapter.ViewHolder> implements View.OnClickListener{

    private Context context;
    private List<Repair> datas;
    private onRecyclerListener listener;

    public RepairAdapter(Context context){
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate( R.layout.item_repair_list, parent,false);
        view.setOnClickListener(this);
        return new RepairAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Repair repair = datas.get(position);
        holder.bindRepo(repair, position);
        holder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public void setOnItemClickListener(onRecyclerListener listener){
        this.listener = listener;
    }

    public void notifyDatasChanged(List<Repair> datas){
        this.datas = datas;

        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        if (listener != null) {
        listener.onItemClicked(v, (int)v.getTag());
    }
}

    public class ViewHolder extends RecyclerView.ViewHolder {

        private  TextView tv_order_name;
        private TextView tv_repair_type ;
        private onRecyclerListener listener;
        private LinearLayout ll_repair_list;
        public ViewHolder(View itemView) {
            super(itemView);
            this.listener = listener;

            tv_order_name = (TextView)itemView.findViewById(R.id.tv_repair_id);
            tv_repair_type = (TextView)itemView.findViewById(R.id.tv_repair_type);
            ll_repair_list = (LinearLayout) itemView.findViewById(R.id.ll_repair_list);

        }

        public void bindRepo(Repair repair, int position) {
            tv_order_name.setText(repair.getId_());
            String type = "";
            int status = repair.getOrderType();
            if (status == Constants.STATUS_THREE){
                type = context.getResources().getString(R.string.common_repair);
            }else {
                type = context.getResources().getString(R.string.common_unrepair);
            }
            tv_repair_type.setText(type);
        }


    }

}
